package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.TransactionOtherSettings;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MoneyUtils;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.title.Title;

/**
 * 其它交易控制
 * Created by lj on 2017/2/13.
 */
public class OtherTransactionSettingActivity extends ParentActivity {
    private View mIsCancelTheRefundTransactionInputPassword, mIsAllowManualInputCardNumber, mDefaultTransactionSelect, mMaxRefundAmt, mForceNFCView,mSupportESignature;
    private ButtonSwitch mIsCancelTheRefundTransactionInputPasswordSwitch, mIsAllowManualInputCardNumberSwitch, mDefaultTransactionSelectSwitch, mICCardShowSwitch, isForceNFCSwitch,isSupportESignatureSwitch;
    private EditTextDialog mEditTextDialog;
    private TextView maxReturnAmtRightText;
    private TransactionOtherOperation mTransactionOtherOperation;
    private RelativeLayout ICCardShowCardNumberRl;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_transaction_setting);
        mTransactionOtherOperation = TransactionOtherOperation.getInstance();
        initView();
        setClickListener();
        setCheckedChangeListener();
        updateUI();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Title mTitle;
        TextView mLeftText;
        mTitle = (Title) findViewById(R.id.other_transaction_setting_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.other_transaction_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OtherTransactionSettingActivity.this.finish();
            }
        }, null, null, null);

        mIsCancelTheRefundTransactionInputPassword = findViewById(R.id.is_cancel_the_refund_transaction_input_password);
        mIsAllowManualInputCardNumber = findViewById(R.id.is_allow_manual_input_card_number);
        mDefaultTransactionSelect = findViewById(R.id.default_transaction_select);
        mMaxRefundAmt = findViewById(R.id.max_refund_amt);
        ICCardShowCardNumberRl = (RelativeLayout) findViewById(R.id.IC_car_show_card_number);
        mForceNFCView = findViewById(R.id.isForceNFC);
        mSupportESignature = findViewById(R.id.isSupportESignature);
        mLeftText = (TextView) mIsCancelTheRefundTransactionInputPassword.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.is_cancel_the_refund_transaction_input_password));

        mLeftText = (TextView) mIsAllowManualInputCardNumber.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.is_allow_manual_input_card_number));

        mLeftText = (TextView) mDefaultTransactionSelect.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.default_transaction_select));

        mLeftText = (TextView) mMaxRefundAmt.findViewById(R.id.item_left_text);
        mLeftText.setText(getResources().getString(R.string.max_refund_amt) + getString(R.string.yuan));

        mLeftText = (TextView) mForceNFCView.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.force_nfc));

        mLeftText = (TextView) mSupportESignature.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.support_e_signature));

        maxReturnAmtRightText = (TextView) mMaxRefundAmt.findViewById(R.id.item_right_text);
        maxReturnAmtRightText.setText(getResources().getString(R.string.max_refund_amt2));

        mIsCancelTheRefundTransactionInputPasswordSwitch = (ButtonSwitch) mIsCancelTheRefundTransactionInputPassword.findViewById(R.id.set_switch);
        mIsAllowManualInputCardNumberSwitch = (ButtonSwitch) mIsAllowManualInputCardNumber.findViewById(R.id.set_switch);
        mDefaultTransactionSelectSwitch = (ButtonSwitch) mDefaultTransactionSelect.findViewById(R.id.set_switch);
        mICCardShowSwitch = (ButtonSwitch) findViewById(R.id.IC_car_show_card_number_set_switch);
        isForceNFCSwitch = (ButtonSwitch) mForceNFCView.findViewById(R.id.set_switch);
        isSupportESignatureSwitch = (ButtonSwitch) mSupportESignature.findViewById(R.id.set_switch);
    }

    private void setClickListener() {
        mIsCancelTheRefundTransactionInputPassword.setOnClickListener(this);
        mIsAllowManualInputCardNumber.setOnClickListener(this);
        mDefaultTransactionSelect.setOnClickListener(this);
        mMaxRefundAmt.setOnClickListener(this);
        ICCardShowCardNumberRl.setOnClickListener(this);
        mForceNFCView.setOnClickListener(this);
        mSupportESignature.setOnClickListener(this);
    }

    private void setCheckedChangeListener() {
        mIsCancelTheRefundTransactionInputPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveInputDirectorPsd(isChecked);
            }
        });

        mIsAllowManualInputCardNumberSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveInputCardNum(isChecked);
            }
        });

        mDefaultTransactionSelectSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveDefaultTransaction(isChecked);
            }
        });
        mICCardShowSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveICCardShowCardNumber(isChecked);
            }
        });

        isForceNFCSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveForceNFC(isChecked);
            }
        });

        isSupportESignatureSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionOtherOperation.saveSupportESignature(isChecked);
            }
        });

        updateUI();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.is_cancel_the_refund_transaction_input_password:
                //撤销退货类交易是否输入主管密码
                mIsCancelTheRefundTransactionInputPasswordSwitch.setOnCheck(!mIsCancelTheRefundTransactionInputPasswordSwitch.isChecked());
                break;
            case R.id.is_allow_manual_input_card_number:
                //是否允许手输卡号
                mIsAllowManualInputCardNumberSwitch.setOnCheck(!mIsAllowManualInputCardNumberSwitch.isChecked());
                break;
            case R.id.default_transaction_select:
                //缺省交易选择
                mDefaultTransactionSelectSwitch.setOnCheck(!mDefaultTransactionSelectSwitch.isChecked());
                break;
            case R.id.max_refund_amt:
                //最大退货金额
                mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                        getString(R.string.save), getString(R.string.max_refund_amt) + getString(R.string.fen1), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (checkEmpty(mEditTextDialog.getText())) {
                                    mTransactionOtherOperation.saveMaximumReturnAmount(Long.parseLong(mEditTextDialog.getText()));
                                    mEditTextDialog.dismiss();
                                    updateUI();
                                }
                            }
                        }, new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                showSoftKeyboard(mEditTextDialog);
                                mEditTextDialog.setMaxLength(12);
                            }
                        });
                mEditTextDialog.setHintText(getResources().getString(R.string.fen));
                if (!TextUtils.isEmpty(mTransactionOtherOperation.getTransactionOtherSettings().getMaximumReturnAmount() + "")) {
                    mEditTextDialog.setText(mTransactionOtherOperation.getTransactionOtherSettings().getMaximumReturnAmount() + "");
                }
                mEditTextDialog.show();
                break;
            case R.id.IC_car_show_card_number:
                mICCardShowSwitch.setOnCheck(!mICCardShowSwitch.isChecked());
                break;
            case R.id.isForceNFC:
                isForceNFCSwitch.setOnCheck(!isForceNFCSwitch.isChecked());
                break;
            case R.id.isSupportESignature:
                isSupportESignatureSwitch.setOnCheck(!isSupportESignatureSwitch.isChecked());
                break;
            default:
                break;
        }
    }

    private void updateUI() {
        TransactionOtherSettings transactionOtherSettings = mTransactionOtherOperation.getTransactionOtherSettings();
        mIsCancelTheRefundTransactionInputPasswordSwitch.setOnCheck(transactionOtherSettings.getInputDirectorPsd());
        mIsAllowManualInputCardNumberSwitch.setOnCheck(transactionOtherSettings.getInputCardNum());
        mDefaultTransactionSelectSwitch.setOnCheck(transactionOtherSettings.getDefaultTransaction());
        mICCardShowSwitch.setOnCheck(transactionOtherSettings.isICCardShowCardNumber());
        isSupportESignatureSwitch.setOnCheck(transactionOtherSettings.isSupportESignature());
        if (transactionOtherSettings.getMaximumReturnAmount() < 0) {
            maxReturnAmtRightText.setText(getResources().getString(R.string.max_refund_amt2));
        } else {
            maxReturnAmtRightText.setText(MoneyUtils.longCent2DoubleMoneyStr(transactionOtherSettings.getMaximumReturnAmount()));
        }
        isForceNFCSwitch.setOnCheck(transactionOtherSettings.isForceNFC());

    }
}
